package cn.cloudreal.cbms.Dictionary.service.impl;

import java.util.Iterator;
import java.util.List;

import cn.cloudreal.cbms.Dictionary.dao.DictionaryDao;
import cn.cloudreal.cbms.Dictionary.service.DictionaryService;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.vo.Dictionary;

/**
 * 数据字典操作业务逻辑类
 * 
 * @author Gao_xuejiao
 * @data 2012-07-11
 */
public class DictionaryServiceImpl implements DictionaryService
{

    public DictionaryDao dictionaryDao;

    /**
     * 删除
     */
    public void delete(Dictionary persistentInstance)
    {
    	// TODO Auto-generated method stub
        dictionaryDao.delete(persistentInstance);
        DatadicContext.getInstance().reloadByCode(persistentInstance.getClasscode());
    }
    
    /**
     * 递归删除
     */
    public void recursionDelete(int id)
    {
    	List<Dictionary> sons =  dictionaryDao.findByParentId(id);
    	if(sons.size()==0){
    		/***
    		 * 在此之前应该做全库扫描，看看该数据字典被引用
    		 */
    		dictionaryDao.delete(id);
    		
    	}else{
    		for (Iterator<Dictionary> iterator = sons.iterator(); iterator.hasNext();) {
				Dictionary dictionary =  iterator.next();
				recursionDelete(dictionary.getId());
			}
    	}
    }



    public void save(Dictionary persistentInstance)
    {
        dictionaryDao.save(persistentInstance);
        DatadicContext.getInstance().reloadByCode(persistentInstance.getClasscode());
    }

    public void update(Dictionary persistentInstance)
    {
        dictionaryDao.update(persistentInstance);
        DatadicContext.getInstance().reloadByCode(persistentInstance.getClasscode());
    }

    public DictionaryDao getDictionaryDao()
    {
        return dictionaryDao;
    }

    public void setDictionaryDao(DictionaryDao dictionaryDao)
    {
        this.dictionaryDao = dictionaryDao;
    }

	public List<Dictionary> getAllDictionarys() {
		return dictionaryDao.getAllDictionarys();
	}



	public List<Dictionary> findAll(Dictionary dicTemp) {
		return dictionaryDao.findAll(dicTemp);
	}



	public boolean isUseDictionary(int id) {
		return dictionaryDao.isUseDictionary(id);
	}
	
	public Dictionary findByID(int id){
		return dictionaryDao.findByID(id);
	}



	public Dictionary findByClassCode(String classCode, String id) {
		// TODO Auto-generated method stub
		return dictionaryDao.findByClassCode(classCode, id);
	}



	public Dictionary findByClassName(String className) {
		// TODO Auto-generated method stub
		return dictionaryDao.findByClassName(className);
	}



	public List<Dictionary> findByParentId(int parentID) {
		return dictionaryDao.findByParentId(parentID);
	}

	public Dictionary findByIdAndClassCode(String id, String classCode) {
		// TODO Auto-generated method stub
		return dictionaryDao.findByIdAndClassCode(id, classCode);
	}

	public List<Dictionary> findDicDataList(String classCode, String parentId) {
		// TODO Auto-generated method stub
		return dictionaryDao.findDicDataList(classCode, parentId);
	}

}
